package com.huizhonglm.diap.widget.xGallery.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.ViewGroup;

import com.huizhonglm.diap.widget.xGallery.OnSaveBitmapListener;
import com.huizhonglm.diap.widget.xGallery.view.XTouchView;
import com.huizhonglm.diap.widget.xGallery.view.XViewPager;

import java.util.List;

/**
 Class wraps URLs to adapter, then it instantiates objects to paging up through them.
 */
public class XPagerAdapter extends BasePagerAdapter {
	private Context mContext;
    private String mFlag;
    private OnSaveBitmapListener onSaveListener;


    private OnClickBitmapListener onClickBitmapListener;
	public XPagerAdapter(Context context, String flag, List<String> resources){
		super(context, resources);
        mFlag = flag;
        mContext = context;
	}

    public void setOnSaveBitmapListener(OnSaveBitmapListener l){
        onSaveListener = l;
    }
    public void setOnClickBitmapListener(OnClickBitmapListener l){
        onClickBitmapListener = l;
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        super.setPrimaryItem(container, position, object);
        ((XViewPager)container).mCurrentView = ((XTouchView)object).getImageView();
    }

    @Override
    public Object instantiateItem(ViewGroup collection, final int position){
        final XTouchView iv = new XTouchView(mContext);

        if ("preview".equals(mFlag)) {
            iv.setPath(mResources.get(position),new XTouchView.OnClickCallBack() {
                @Override
                public void onClickCallBack() {
                    if (onClickBitmapListener !=  null){
                        onClickBitmapListener.onCLickBitmap();
                    }
                }
            });
        } else {
            iv.setUrl(mResources.get(position),new XTouchView.OnSaveCallBack() {
                @Override
                public void onSaveCallBack(Bitmap bmp) {
                    if (onSaveListener != null) {
                        onSaveListener.onSaveBitmap(bmp);
                    }
                }
            },new XTouchView.OnClickCallBack() {
                @Override
                public void onClickCallBack() {
                   if (onClickBitmapListener !=  null){
                       onClickBitmapListener.onCLickBitmap();
                   }
                }
            });
        }
        iv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        collection.addView(iv, 0);
        return iv;
    }

    public interface OnClickBitmapListener{
        public void onCLickBitmap();
    }
}
